<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <link rel="stylesheet" href="/layui/css/layui.css">
  <style>
    .layui-card-body {
      height: 950px;
    }
  </style>
</head>

<body>
  <div class="layui-card">
    <div class="layui-card-header">
      <span class="layui-breadcrumb">
        <a href="">项目管理</a>
        <a><cite>修改项目</cite></cite></a>
      </span>
    </div>
    <div class="layui-card-body">
      <form class="layui-form" action="" style="width: 800px;height: 400px;">
        <input type="hidden" value="<%=data.project_id%>" name="project_id">
        <!-- 项目分类 -->
        <div class="layui-form-item">
          <label class="layui-form-label">项目分类：</label>
          <div class="layui-input-block">
            <select name="pc_id" lay-verify="">
              <option value="<%= data.pc_id%>">
                <%= data.pc_name%>
              </option>
              <option value="">请选择项目分类</option>
              <% category.forEach((item)=>{%>
                <option value="<%=item.pc_id%>">
                  <%=item.pc_name%>
                </option>
                <%}) %>
            </select>


            <!-- 更改所属分类，计划用连表查询，选择框来做 -->
            <!-- <input type="text" name="pc_name" value="<%= data.pc_name%>" class="layui-input" disabled> -->
            <!-- 隐藏域，传id -->
            <!-- <input type="hidden" name="pc_id" value="<%= data.pc_id%>"> -->
          </div>
        </div>
        <!-- 项目名称 -->
        <div class="layui-form-item">
          <label class="layui-form-label">项目名称：</label>
          <div class="layui-input-block">
            <input type="text" name="project_title" required lay-verify="required" placeholder="请输入项目名称"
              autocomplete="off" class="layui-input" value="<%=data.project_title %>">
          </div>
        </div>
        <!-- 上传图片 -->
        <div class="layui-form-item">
          <label class="layui-form-label">项目图片：</label>
          <div class="layui-input-block">
            <button type="button" class="layui-btn" id="thumb">
              <i class="layui-icon">&#xe67c;</i>上传图片
            </button>
          </div>
        </div>
        <div class="layui-form-item">
          <div class="layui-input-block">
            <blockquote class="layui-elem-quote">预览图
              <div>
                <img src="<%=data.project_thumb%>" alt="" class="thumb" style="width:200px;">
              </div>
              <!-- 隐藏域,如果有更改图片，就需要把旧图片的地址传过去 -->
              <input type="hidden" name="delete_project_thumb" value="<%=data.project_thumb%>">
              <!-- 用来存储新图片的地址 -->
              <input type="hidden" name="new_project_thumb">
            </blockquote>
          </div>
        </div>
        <!-- 项目描述 -->
        <div class="layui-form-item">
          <label class="layui-form-label">项目描述：</label>
          <div class="layui-input-block">
            <textarea style="display: none;" id="demo" name="project_content"><%=data.project_content %></textarea>
          </div>
        </div>
        <!-- 项目组图 -->
        <div class="layui-form-item">
          <label class="layui-form-label">项目组图：</label>
          <div class="layui-input-block">
            <button type="button" class="layui-btn" id="pthumb">
              <i class="layui-icon">&#xe67c;</i>上传项目组图
            </button>
          </div>
        </div>
        <div class="layui-form-item">

          <div class="layui-input-block">
            <blockquote class="layui-elem-quote">预览图
              <div class="imglist">
                <% data.project_banner.split(",").forEach((item)=>{ %>
                  <img width="200px" src="<%=item%>" alt="" class="oldclass">
                  <%})%>
              </div>
              <!-- 隐藏域，保存新组图的地址 -->
              <input type="hidden" name="new_project_banner">
              <!-- 隐藏域，保存旧组图的位置 -->
              <input type="hidden" name="delete_project_banner" value="<%=data.project_banner%>">
            </blockquote>
          </div>
        </div>
        <!-- 提交按钮 -->
        <div class="layui-form-item">
          <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="projectform">提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
          </div>
        </div>
      </form>
    </div>
  </div>
</body>

</html>

<script src="/layui/layui.js"></script>
<script>
  var $

  layui.use(['element', 'upload', 'jquery', 'layer', 'form', 'layedit'], function () {
    var { element, upload, layer, form, layedit } = layui
    $ = layui.$
    // js方式
    // var thumb = document.querySelector(".thumb")
    // var project_thumb = document.querySelector("[name = project_thumb]")

    //缩略图上传
    upload.render({
      elem: "#thumb",
      url: "/admin/project/upload",
      done: (res) => {
        if (res.code == 1) {
          //吧图片路径给缩略图
          $(".thumb").attr("src", res.url)
          //将新图的url保存到input中
          $("[name = new_project_thumb]").val(res.url)
        } else {
          layer.msg("图片上传失败")
        }

      }
    })


    //富文本编辑器
    var layeditIns = layedit.build('demo', {

    }); //建立编辑器

    //上传组图
    var imglist = document.querySelector(".imglist")


    var pthumbList = []
    //组图上传
    upload.render({
      elem: "#pthumb",
      url: "/admin/project/upload",
      multiple: true,
      number: 4,
      done: (res) => {
        if (res.code == 1) {
          //原图片进行掩藏
          $(".oldclass").css("display", "none")
          //jquery的方法
          $("<img width='200'>").attr("src", res.url).appendTo($(".imglist"))
          pthumbList.push(res.url)
          $("[name=new_project_banner]").val(pthumbList.join(","))
        }
      }

    })


    form.on("submit(projectform)", data => {
      data.field.project_content = layedit.getContent(layeditIns)
      $.ajax({
        type: "POST",
        url: "/admin/project/update",
        data: data.field,
        success: (res) => {
          var { code, msg } = res

          layer.msg(msg)
        }
      })
      return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
    })

  })
</script>